//
//  XJYMyTableView.m
//  ShopMall
//
//  Created by XJY on 17/1/18.
//  Copyright © 2017年 XinJinYing. All rights reserved.
//

#import "XJYMyTableView.h"
#import "XJYMyTableViewCell.h"

@interface XJYMyTableView ()<UITableViewDelegate, UITableViewDataSource>

//数据源
@property (nonatomic, strong) NSArray *messageArr;

@end


@implementation XJYMyTableView


- (NSArray *)messageArr
{
    //这里也可以写成plist文件
    if (!_messageArr) {
        _messageArr = @[@{@"image":@"我的界面我的收藏图标",@"title":@"我的收藏"},
                        @{@"image":@"我的界面意见反馈图标",@"title":@"意见反馈"},
                        @{@"image":@"我的界面关于我们图标",@"title":@"关于我们"},
                        @{@"image":@"我的界面客服热线图标",@"title":@"客服热线"},
                        @{@"image":@"我的界面我的优惠券图标",@"title":@"我的优惠券"},
                        @{@"image":@"我的界面邀请好友图标",@"title":@"邀请好友,立刻赚钱"}];
    }
    return _messageArr;
}

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style
{
    self = [super initWithFrame:frame style:style];
    if (self) {
        self.delegate = self;
        self.dataSource = self;
        //self.bounces = NO;
        self.backgroundColor = MainColor;
    }
    return self;
}


#pragma mark - tableview的代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSNumber *flag = [[NSUserDefaults standardUserDefaults] objectForKey:@"ISLOGIN"];
    if(flag) {
        return 6;
    } else {
        return 4;
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    XJYMyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[XJYMyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    //cell.textLabel.text = [@(indexPath.row) stringValue];
    cell.myDic = self.messageArr[indexPath.row];
    
    if (indexPath.row == 3) {
        //利用KVC暴力的拿到cell中的箭头
        UIImageView *nextImage = [cell valueForKey:@"nextImage"];
        //此处为什么不直接移除，需要隐藏呢？
        //因为移除掉之后，再在cell中添加了label，要重新走cell中layoutSubviews的方法，需要给nextimage布局，
        //这样就会造成继续访问nextimage,此时实际已被移除，所以会崩
        
        [nextImage setHidden:YES];
        
        UILabel *phone = [[UILabel alloc] init];
        phone.textColor = RGB(123, 124, 128);
        phone.text = @"400-100-1212";
        [cell addSubview:phone];
        
        __weak typeof(cell) weakSelf = cell;
        [phone mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(weakSelf.mas_right).offset(-15);
            make.centerY.equalTo(weakSelf.mas_centerY);
            make.size.mas_equalTo(CGSizeMake(110, 15));
        }];
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    NSLog(@"我点击了%ld",(long)indexPath.row);
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 160;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    NSNumber *flag = [[NSUserDefaults standardUserDefaults] objectForKey:@"ISLOGIN"];
    if (flag) {
        UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, 160)];
        backView.backgroundColor = MainColor;
        
        UIButton *exitLoginButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [exitLoginButton setTitle:@"退出登录" forState:UIControlStateNormal];
        [exitLoginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        exitLoginButton.titleLabel.font = [UIFont systemFontOfSize:18];
        [exitLoginButton addTarget:self action:@selector(exitLoginClick:) forControlEvents:UIControlEventTouchUpInside];
        [exitLoginButton setBackgroundColor:RGB(56, 166, 241)];
        exitLoginButton.frame = CGRectMake(30, 30, VIEW_WIDTH - 60, 50);
        [backView addSubview:exitLoginButton];
        
        return backView;
    }
    return nil;
}

- (void)exitLoginClick:(UIButton *)button
{
    if (_exitLoginBlock) {
        _exitLoginBlock();
    }
}

@end
